/**
	License:		Artistic license 2.0 (see license.txt, also available online at http://www.perlfoundation.org/artistic_license_2_0)
	Authors:		OrbitalLab (http://www.orbitallab.ru/moonglide/), 2007-2009

	File:			OpenGLLinux.d
	Description:	all Linux-dependent stuff for OpenGL, slightly separated from window management
	Date:			11.04.2008 by Digited
*/
module moonglide.renderer.opengl.platform.OpenGLLinux;

version( linux )
{

import
	derelict.opengl.glx,

	moonglide.util.MoonUtil,

	moonglide.renderer.opengl.GL,
	moonglide.renderer.IRendererOS,

	moonglide.os.linux.LinuxOS;


//===============================================================
/// OpenGLWin32 - class with OS specified code for opengl renderer
//===============================================================
class COpenGLLinux : IRendererOS
{
	protected:
		CLinuxOS	_ios;
		bool		_initialized,
					_deinitialized;

	public:
		//===============================================================
		/// Initialize - init stuff, os dependent (win32). Look at parameters and guess what does it do.
		//===============================================================
		bool initialize(
			char[]			windowName,
			inout Size2ui	windowSize,
			ubyte			bpp,
			ubyte			refreshFreq,
			bool			fullscreen,
			bool 			vsync,
			bool 			antialiasing,
			bool 			stencil )
		{
			if( !_ios.createPlatformWindow( windowSize, fullscreen, windowName ) )
			{
				errorLog( "Renderer: ios returned error on creating a window" );
				return false;
			}

			DerelictGL.loadExtensions();

			if( fullscreen )
			{
				if( !_ios.changeScreenResolution( windowSize.x, windowSize.y, bpp, fullscreen, refreshFreq ) )
				{
					errorLog( "Renderer: ios returned error on changing screen resolution" );
					return false;
				}
			}

			_initialized = true;
			_deinitialized = false;

			traceLog.trace( "Platform OpenGL initialization done." );

			return true;
		}

		//===============================================================
		/// DeInitialize - release & destroy os-dependent stuff
		//===============================================================
		bool deInitialize()
		{
			if(( !_initialized ) || _deinitialized )
			{
				errorLog( "COpenGLLinux: DeInit called while not Initialized or already Deinitialized!" );
				return false;
			}

			debug traceLog.trace( "COpenGLLinux DeInit" );
			_ios.shutdown();

			_deinitialized = true;

			return true;
		}

		//===============================================================
		/// Present frame - swap buffers, end of rendering. Must be called last while processing scene.
		//===============================================================
		bool presentFrame()
		{
			glXSwapBuffers( g_pDisplayGL, cast(GLXDrawable)g_window );
			return true;
		}


		this( IOS ios )
		{
			moonAssert( cast(CLinuxOS)ios !is null, "ios is not CLinuxOS!", __FILE__, __LINE__ );

			_ios = cast(CLinuxOS)ios;
		}

		~this()
		{
			deInitialize();
		}

		void printPlatformText( char[] line, Vector2f position, Color4f color ) {}
} // end class COpenGLLinux


} // end version( linux )
